package com.example.appstorenew;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.GridView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;


import com.example.appstorenew.adapter.appRecommendAdapter;
import com.example.appstorenew.pojo.appRecommend;
import com.example.appstorenew.utils.AppRecommendUtil;
import com.example.appstorenew.utils.TokenUtil;

import java.util.List;

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener, View.OnClickListener {


    List<appRecommend> appRecommendList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button bt_search = findViewById(R.id.bt_search);
        Button bt_update = findViewById(R.id.bt_update);

        bt_search.setOnClickListener(this);
        bt_update.setOnClickListener(this);
        //适配器填满APP推荐列表
        GridView gv_tuijianapp = findViewById(R.id.gv_tuijianapp);
//    静态数据获取list
//        appRecommendList = appRecommend.getAppRecommentList();
//        动态数据获取list
        new Thread(()->{
            try {
                String token = TokenUtil.getToken();
                appRecommendList = AppRecommendUtil.getAppRecommendList(token);

                runOnUiThread(()->{
                    appRecommendAdapter appRecommendAdapter = new appRecommendAdapter(this,appRecommendList);
                    gv_tuijianapp.setAdapter(appRecommendAdapter);
                });
            } catch (Exception e) {
                e.printStackTrace();
            }

        }).start();


        gv_tuijianapp.setOnItemClickListener(this);


    }



    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Toast.makeText(this,"您下载了："+appRecommendList.get(position).getApkName()+"!",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bt_search:
                startActivity(new Intent(this, SearchActivity.class));
                break;
            case R.id.bt_update:
                GridLayout gl_update = findViewById(R.id.gl_update);
                gl_update.setVisibility(View.VISIBLE);
                break;

        }
    }
}